﻿using ASmile.DBModel;
using ASmile.Extend;
using ASmile.ORM.Cores;
using System.Collections.Generic;
using System.Linq;

namespace ASmile.Com.Da
{
    public class PrintConfigBLL : BLLCrudBase<Sys_PrintConfig>, IPrintConfigBLL
    {
        protected override void SetQuery(QueryCondition<Sys_PrintConfig> query, ArgPageData arg)
        {
            if (arg.TagData != null)
            {
                query.WhereAnd(s => s.FormName == arg.TagData.ToString());
            }

            if (!arg.SearchVal.IsEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(
                    s =>
                    s.WEx_Like(s.PrinterName, likeStr) ||
                    s.WEx_Like(s.TemplateFile, likeStr)
                );
            }
            query.WhereAnd(s => s.ClientId == "0" || s.ClientId == CurrClientId);
        }

        protected override ApiResult<string> VerifyModel(Sys_PrintConfig model, bool isAdd)
        {
            if (model.FormName.IsEmpty())
            {
                return RetErr(nameof(model.FormName), "窗体名称不能为空");
            }
            if (model.Caption.IsEmpty())
            {
                return RetErr(nameof(model.Caption), "标题不能为空");
            }
            if (model.TemplateFile.IsEmpty())
            {
                return RetErr(nameof(model.TemplateFile), "模板文件不能为空");
            }
            //窗体序号不能为空
            if (DB.QueryExist<Sys_PrintConfig>(s =>
                 s.Id != model.Id &&
                 s.ClientId == model.ClientId &&
                 s.FormName == model.FormName &&
                 s.PrintIndex == model.PrintIndex))
            {
                return RetErr(nameof(model.FormName), "已包含当前窗体与序号的配置");
            }

            if (model.IsLocal)
            {
                model.ClientId = CurrClientId;
            }
            else
            {
                model.ClientId = "0";
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            else
            {
                return RetOK(string.Empty, "更新成功");
            }
        }

        public ApiResult<Sys_PrintConfig> GetPrintConfig(string formName, int printIndex)
        {
            var rList = DB.Query<Sys_PrintConfig>(s => s.FormName == formName && s.PrintIndex == printIndex).ToList();
            Sys_PrintConfig rModel;
            if (rList == null)
            {
                rModel = null;
            }
            else if (rList.Count == 1)
            {
                rModel = rList[0];
            }
            else
            {
                rModel = rList.Find(s => s.ClientId == CurrClientId);
            }
            return RetOK(rModel);
        }

        public ApiResultList<Sys_PrintConfig> GetPrintConfigs(string formName)
        {
            var cfgList = DB.Query<Sys_PrintConfig>(s => s.FormName == formName).ToList();
            if (cfgList == null)
            {
                return RetOK(cfgList, 0);
            }

            cfgList = cfgList.FindAll(s => s.IsLocal == false || (s.IsLocal == true && s.ClientId == CurrClientId));
            List<Sys_PrintConfig> rList = new List<Sys_PrintConfig>();
            var rGroups = cfgList.GroupBy(s => s.PrintIndex);
            foreach (var item in rGroups)
            {
                if (item.Count() == 1)
                {
                    //如果单个取默认
                    rList.Add(item.First());
                }
                else
                {
                    //如果有多个，取本地客户端配置（即忽略了非本地配置）
                    rList.Add(item.First(s => s.IsLocal == false));
                }
            }

            return RetOK(rList, rList.Count);

        }
    }
}